package com.yuke.cloud.service.cmc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.RedisKeyUtil;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillAndSkuDto;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillDto;
import com.yuke.cloud.service.api.cmc.dto.CmsSecKillSkuDto;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.cmc.dao.CmsSecKillMapper;
import com.yuke.cloud.service.cmc.dao.CmsSecKillSkuMapper;
import com.yuke.cloud.service.cmc.entity.CmsSecKill;
import com.yuke.cloud.service.cmc.entity.CmsSecKillSku;
import com.yuke.cloud.service.cmc.service.CmsSecKillService;
import com.yuke.cloud.service.cmc.service.CmsStorageSpecService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


/**
 * Created by wg on 2019/05/28.
 */
@Service
@Transactional
public class CmsSecKillServiceImpl extends BaseService<CmsSecKill> implements CmsSecKillService {
    @Resource
    private CmsSecKillMapper cmsSecKillMapper;

    @Resource
    private CmsSecKillSkuMapper cmsSecKillSkuMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private PmcFeignApi pmcFeignApi;

    @Resource
    private SmcFeignApi smcFeignApi;

    @Resource
    private WmcStorageFeignApi wmcFeignApi;

    @Resource
    private CmsStorageSpecService cmsStorageSpecService;


    @Override
    public void deleteById(Long id) {
        Boolean bDelete = false;

        // 判断该秒杀活动是否正在进行中，如果是则无法删除
        CmsSecKillDto cmsSecKillDto = (CmsSecKillDto) redisTemplate.opsForValue().get(RedisKeyUtil.getSecKillKey(String.valueOf(id)));

        if (cmsSecKillDto == null) {  // 活动一定不在进行中，可以删除
            bDelete = true;
        } else {
            Date now = new Date();
            if (cmsSecKillDto.getStartTime().getTime() > now.getTime() || now.getTime() > cmsSecKillDto.getEndTime().getTime()) {
                bDelete = true;
            }
        }

        //Preconditions.checkState(bDelete, "该秒杀活动正在进行中，无法删除");

        // 先删除该秒杀活动下的商品
        Example example = new Example(CmsSecKillSku.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("secKillId", id);
        cmsSecKillSkuMapper.deleteByExample(example);

        super.deleteById(id);

        // 删除缓存
        if (cmsSecKillDto != null) {
            // 删除商品及用户购买信息
            Set<String> keys = redisTemplate.keys(RedisKeyUtil.getSecKillSkuPrexKey(String.valueOf(id)));
            if (CollectionUtils.isNotEmpty(keys)) {
                redisTemplate.delete(keys);
            }else{
                Preconditions.checkState(false,"该时间不合法");
            }


            redisTemplate.delete(RedisKeyUtil.getSecKillKey(String.valueOf(id)));//根据key删除缓存
        }
    }

    @Override
    public int update(CmsSecKill cmsSecKill) {
        Boolean bUpdate = false;

        // 判断该秒杀活动是否正在进行中，如果是则无法删除
        CmsSecKillDto cmsSecKillDto = (CmsSecKillDto) redisTemplate.opsForValue().get(RedisKeyUtil.getSecKillKey(String.valueOf(cmsSecKill.getSecKillId())));

        /*if (cmsSecKillDto == null) {  // 活动一定不在进行中，可以更新
            bUpdate = true;
        } else {
            Date now = new Date();
            if (cmsSecKillDto.getStartTime().getTime() > now.getTime() || now.getTime() > cmsSecKillDto.getEndTime().getTime()) {
                bUpdate = true;
            }
        }*/

        //Preconditions.checkState(bUpdate, "该秒杀活动正在进行中，无法修改");

        int ret = super.update(cmsSecKill);

        // 更新缓存
        if (cmsSecKillDto != null) {
            BeanUtil.copyProperties(cmsSecKill, cmsSecKillDto, CopyOptions.create().setIgnoreNullValue(true));

            Date now = new Date();
            long timeOut = cmsSecKillDto.getEndTime().getTime() - now.getTime();
            if (timeOut>0){
                redisTemplate.opsForValue().set(RedisKeyUtil.getSecKillKey(String.valueOf(cmsSecKill.getSecKillId())), cmsSecKillDto, timeOut, TimeUnit.MILLISECONDS);
            } else{
                Preconditions.checkState(false,"该时间不合法");
            }



        }

        return ret;
    }

    @Override
    public void deleteByIds(String ids) {
        List<Long> secKillIds = Arrays.asList(ids.split(",")).stream().map(id -> Long.valueOf(id)).collect(Collectors.toList());
        Boolean bDelete = true;
        for (Long id : secKillIds) {
            if (isSecKillOn(id)) {
                bDelete = false;
                break;
            }
        }

        //Preconditions.checkState(bDelete, "要删除的秒杀活动有部分正在进行中，无法删除");

        for (Long id : secKillIds) {
            this.deleteById(id);
        }
    }

    // 判断秒杀活动是否正在进行中
    @Override
    public Boolean isSecKillOn(Long secKillId) {
        Boolean bSecKillOn = true;
        // 判断该秒杀活动是否正在进行中，如果是则无法删除
        CmsSecKillDto cmsSecKillDto = (CmsSecKillDto) redisTemplate.opsForValue().get(RedisKeyUtil.getSecKillKey(String.valueOf(secKillId)));

        if (cmsSecKillDto == null) {  // 活动一定不在进行中，可以删除
            bSecKillOn = false;
        } else {
            Date now = new Date();
            if (cmsSecKillDto.getStartTime().getTime() > now.getTime() || now.getTime() > cmsSecKillDto.getEndTime().getTime()) {
                bSecKillOn = false;
            }
        }

        return bSecKillOn;
    }

    @Override
    public List<CmsSecKillAndSkuDto> getNextSecKillAndSpecs() {

        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = new ArrayList<>();
        Example example = new Example(CmsSecKill.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("start_time");
        criteria.andLessThan("secKillType",3);
        Date start = new Date();
        Date end = new Date();
        end.setTime(start.getTime() + 24 * 60 * 60 * 1000 - 1000);

        //criteria.andBetween("startTime", start, end);
        /*criteria.andCondition("start_time<"+start.getTime());
        criteria.andCondition("end_time>"+start.getTime());*/
        criteria.andCondition(" UNIX_TIMESTAMP(start_time)<="+start.getTime()/1000);
        criteria.andCondition(" UNIX_TIMESTAMP(end_time)>="+start.getTime()/1000);
       
        List<CmsSecKill> cmsSecKills = cmsSecKillMapper.selectByExample(example);

        if (cmsSecKills != null && cmsSecKills.size() > 0) {
            List<Long> ids = new ArrayList<>();
            for (CmsSecKill cmsSecKill : cmsSecKills) {
                ids.add(cmsSecKill.getSecKillId());
                CmsSecKillAndSkuDto cmsSecKillAndSkuDto = new CmsSecKillAndSkuDto();
                CmsSecKillDto cmsSecKillDto = new CmsSecKillDto();
                BeanUtils.copyProperties(cmsSecKill, cmsSecKillDto);
                cmsSecKillAndSkuDto.setCmsSecKillDto(cmsSecKillDto);
                cmsSecKillAndSkuDtos.add(cmsSecKillAndSkuDto);
            }

            Example exampleSku = new Example(CmsSecKillSku.class);
            exampleSku.setOrderByClause("sort");
            Example.Criteria criteriaSku = exampleSku.createCriteria();
            criteriaSku.andIn("secKillId", ids);
            List<CmsSecKillSku> cmsSecKillSkus = cmsSecKillSkuMapper.selectByExample(exampleSku);
            if (cmsSecKillSkus != null && cmsSecKillSkus.size() > 0) {
                // 获取商品信息
                getSpecInfo(cmsSecKillSkus);

                for (CmsSecKillAndSkuDto cmsSecKillAndSkuDto : cmsSecKillAndSkuDtos) {
                    List<CmsSecKillSkuDto> cmsSecKillSkuDtos = new ArrayList<>();
                    for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
                        if (cmsSecKillAndSkuDto.getCmsSecKillDto().getSecKillId().longValue() == cmsSecKillSku.getSecKillId().longValue()) {
                            CmsSecKillSkuDto cmsSecKillSkuDto = new CmsSecKillSkuDto();
                            BeanUtils.copyProperties(cmsSecKillSku, cmsSecKillSkuDto);
                            cmsSecKillSkuDtos.add(cmsSecKillSkuDto);
                        }
                    }
                    cmsSecKillAndSkuDto.setCmsSecKillSkuDtos(cmsSecKillSkuDtos);
                }
            }
        }

        return cmsSecKillAndSkuDtos;
    }

    @Override
    public List<CmsSecKillAndSkuDto> getSecKillAndSpecs() {
        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = new ArrayList<>();
        Example example = new Example(CmsSecKill.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("start_time");
        Date now = new Date();
        criteria.andEqualTo("isEnable", true);
        criteria.andCondition(" UNIX_TIMESTAMP( start_time ) <= " + now.getTime() / 1000 + " AND " + now.getTime() / 1000
                + " <= UNIX_TIMESTAMP( end_time ) ");
        List<CmsSecKill> cmsSecKills = cmsSecKillMapper.selectByExample(example);

        if (cmsSecKills != null && cmsSecKills.size() > 0) {
            List<Long> ids = new ArrayList<>();
            for (CmsSecKill cmsSecKill : cmsSecKills) {
                ids.add(cmsSecKill.getSecKillId());
                CmsSecKillAndSkuDto cmsSecKillAndSkuDto = new CmsSecKillAndSkuDto();
                CmsSecKillDto cmsSecKillDto = new CmsSecKillDto();
                BeanUtils.copyProperties(cmsSecKill, cmsSecKillDto);
                cmsSecKillAndSkuDto.setCmsSecKillDto(cmsSecKillDto);
                cmsSecKillAndSkuDtos.add(cmsSecKillAndSkuDto);
            }

            Example exampleSku = new Example(CmsSecKillSku.class);
            exampleSku.setOrderByClause("sort");
            Example.Criteria criteriaSku = exampleSku.createCriteria();
            criteriaSku.andIn("secKillId", ids);
            criteriaSku.andEqualTo("isEnable",true);

            List<CmsSecKillSku> cmsSecKillSkus = cmsSecKillSkuMapper.selectByExample(exampleSku);
            if (cmsSecKillSkus != null && cmsSecKillSkus.size() > 0) {
                // 获取商品信息
                getSpecInfo(cmsSecKillSkus);

                for (CmsSecKillAndSkuDto cmsSecKillAndSkuDto : cmsSecKillAndSkuDtos) {
                    List<CmsSecKillSkuDto> cmsSecKillSkuDtos = new ArrayList<>();
                    for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
                        if (cmsSecKillAndSkuDto.getCmsSecKillDto().getSecKillId().longValue() == cmsSecKillSku.getSecKillId().longValue()) {
                            CmsSecKillSkuDto cmsSecKillSkuDto = new CmsSecKillSkuDto();
                            BeanUtils.copyProperties(cmsSecKillSku, cmsSecKillSkuDto);
                            cmsSecKillSkuDtos.add(cmsSecKillSkuDto);
                        }
                    }
                    cmsSecKillAndSkuDto.setCmsSecKillSkuDtos(cmsSecKillSkuDtos);
                }
            }
        }

        return cmsSecKillAndSkuDtos;
    }

    @Override
    public List<CmsSecKillAndSkuDto> getNowSecKillAndSpecs(Integer count) {

        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = new ArrayList<>();
        Example example = new Example(CmsSecKill.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("start_time");

        Date now = new Date();
        criteria.andLessThan("secKillType", 3);
        criteria.andEqualTo("isEnable", true);
        criteria.andCondition(" UNIX_TIMESTAMP( start_time ) <= " + now.getTime() / 1000 + " AND " + now.getTime() / 1000
                + " <= UNIX_TIMESTAMP( end_time ) ");

        List<CmsSecKill> cmsSecKills = cmsSecKillMapper.selectByExample(example);

        if (cmsSecKills != null && cmsSecKills.size() > 0) {
            List<Long> ids = new ArrayList<>();
            for (CmsSecKill cmsSecKill : cmsSecKills) {
                ids.add(cmsSecKill.getSecKillId());
                CmsSecKillAndSkuDto cmsSecKillAndSkuDto = new CmsSecKillAndSkuDto();
                CmsSecKillDto cmsSecKillDto = new CmsSecKillDto();
                BeanUtils.copyProperties(cmsSecKill, cmsSecKillDto);
                cmsSecKillAndSkuDto.setCmsSecKillDto(cmsSecKillDto);
                cmsSecKillAndSkuDtos.add(cmsSecKillAndSkuDto);
            }

            Example exampleSku = new Example(CmsSecKillSku.class);
            exampleSku.setOrderByClause("sort");
            Example.Criteria criteriaSku = exampleSku.createCriteria();
            criteriaSku.andIn("secKillId", ids);
            if (count != null && count > 0) {
                PageHelper.startPage(0, count);
            }
            List<CmsSecKillSku> cmsSecKillSkus = cmsSecKillSkuMapper.selectByExample(exampleSku);

            if (cmsSecKillSkus != null && cmsSecKillSkus.size() > 0) {
                // 获取商品信息
                getSpecInfo(cmsSecKillSkus);

                for (CmsSecKillAndSkuDto cmsSecKillAndSkuDto : cmsSecKillAndSkuDtos) {
                    List<CmsSecKillSkuDto> cmsSecKillSkuDtos = new ArrayList<>();
                    for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
                        if (cmsSecKillAndSkuDto.getCmsSecKillDto().getSecKillId().longValue() == cmsSecKillSku.getSecKillId().longValue()) {
                            CmsSecKillSkuDto cmsSecKillSkuDto = new CmsSecKillSkuDto();
                            BeanUtils.copyProperties(cmsSecKillSku, cmsSecKillSkuDto);
                            cmsSecKillSkuDtos.add(cmsSecKillSkuDto);
                        }
                    }
                    cmsSecKillAndSkuDto.setCmsSecKillSkuDtos(cmsSecKillSkuDtos);
                    //System.out.println(cmsSecKillAndSkuDto);
                }
            }
        }
        System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzzz"+cmsSecKillAndSkuDtos);
        return cmsSecKillAndSkuDtos;
    }

    @Override
    public List<CmsSecKillAndSkuDto> getLastSecKillAndSpecs(Integer count) {

        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = new ArrayList<>();
        Example example = new Example(CmsSecKill.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("end_time desc");

        Date now = new Date();

        criteria.andEqualTo("isEnable", true);
        criteria.andCondition(" UNIX_TIMESTAMP( end_time ) < " + now.getTime() / 1000 );

        List<CmsSecKill> cmsSecKills = cmsSecKillMapper.selectByExample(example);

        if (cmsSecKills != null && cmsSecKills.size() > 0) {
            List<Long> ids = new ArrayList<>();
            CmsSecKill cmsSecKill = cmsSecKills.get(0);
            ids.add(cmsSecKill.getSecKillId());
            CmsSecKillAndSkuDto cmsSecKillAndSkuDto = new CmsSecKillAndSkuDto();
            CmsSecKillDto cmsSecKillDto = new CmsSecKillDto();
            BeanUtils.copyProperties(cmsSecKill, cmsSecKillDto);
            cmsSecKillAndSkuDto.setCmsSecKillDto(cmsSecKillDto);
            cmsSecKillAndSkuDtos.add(cmsSecKillAndSkuDto);

            Example exampleSku = new Example(CmsSecKillSku.class);
            exampleSku.setOrderByClause("sort");
            Example.Criteria criteriaSku = exampleSku.createCriteria();
            criteriaSku.andIn("secKillId", ids);
            if (count != null && count > 0) {
                PageHelper.startPage(0, count);
            }
            List<CmsSecKillSku> cmsSecKillSkus = cmsSecKillSkuMapper.selectByExample(exampleSku);
            if (cmsSecKillSkus != null && cmsSecKillSkus.size() > 0) {
                // 获取商品信息
                getSpecInfo(cmsSecKillSkus);

                for (CmsSecKillAndSkuDto cmsSecKillAndSkuDtoTemp : cmsSecKillAndSkuDtos) {
                    List<CmsSecKillSkuDto> cmsSecKillSkuDtos = new ArrayList<>();
                    for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
                        if (cmsSecKillAndSkuDtoTemp.getCmsSecKillDto().getSecKillId().longValue() == cmsSecKillSku.getSecKillId().longValue()) {
                            CmsSecKillSkuDto cmsSecKillSkuDto = new CmsSecKillSkuDto();
                            BeanUtils.copyProperties(cmsSecKillSku, cmsSecKillSkuDto);
                            cmsSecKillSkuDtos.add(cmsSecKillSkuDto);
                        }
                    }
                    cmsSecKillAndSkuDtoTemp.setCmsSecKillSkuDtos(cmsSecKillSkuDtos);
                }
            }
        }

        return cmsSecKillAndSkuDtos;
    }

    @Override
    public List<CmsSecKillAndSkuDto> getNextSecKillAndSpecsExt(Integer count) {

        List<CmsSecKillAndSkuDto> cmsSecKillAndSkuDtos = new ArrayList<>();
        Example example = new Example(CmsSecKill.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("start_time");

        Date now = new Date();

        criteria.andEqualTo("isEnable", true);
        criteria.andCondition(" UNIX_TIMESTAMP( start_time ) > " + now.getTime() / 1000 );

        List<CmsSecKill> cmsSecKills = cmsSecKillMapper.selectByExample(example);

        if (cmsSecKills != null && cmsSecKills.size() > 0) {
            List<Long> ids = new ArrayList<>();
            CmsSecKill cmsSecKill = cmsSecKills.get(0);
            ids.add(cmsSecKill.getSecKillId());
            CmsSecKillAndSkuDto cmsSecKillAndSkuDto = new CmsSecKillAndSkuDto();
            CmsSecKillDto cmsSecKillDto = new CmsSecKillDto();
            BeanUtils.copyProperties(cmsSecKill, cmsSecKillDto);
            cmsSecKillAndSkuDto.setCmsSecKillDto(cmsSecKillDto);
            cmsSecKillAndSkuDtos.add(cmsSecKillAndSkuDto);

            Example exampleSku = new Example(CmsSecKillSku.class);
            exampleSku.setOrderByClause("sort");
            Example.Criteria criteriaSku = exampleSku.createCriteria();
            criteriaSku.andIn("secKillId", ids);
            if (count != null && count > 0) {
                PageHelper.startPage(0, count);
            }
            List<CmsSecKillSku> cmsSecKillSkus = cmsSecKillSkuMapper.selectByExample(exampleSku);

            if (cmsSecKillSkus != null && cmsSecKillSkus.size() > 0) {
                // 获取商品信息
                getSpecInfo(cmsSecKillSkus);

                for (CmsSecKillAndSkuDto cmsSecKillAndSkuDtoTemp : cmsSecKillAndSkuDtos) {
                    List<CmsSecKillSkuDto> cmsSecKillSkuDtos = new ArrayList<>();
                    for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
                        if (cmsSecKillAndSkuDtoTemp.getCmsSecKillDto().getSecKillId().longValue() == cmsSecKillSku.getSecKillId().longValue()) {
                            CmsSecKillSkuDto cmsSecKillSkuDto = new CmsSecKillSkuDto();
                            BeanUtils.copyProperties(cmsSecKillSku, cmsSecKillSkuDto);
                            cmsSecKillSkuDtos.add(cmsSecKillSkuDto);
                        }
                    }
                    cmsSecKillAndSkuDtoTemp.setCmsSecKillSkuDtos(cmsSecKillSkuDtos);
                }
            }
        }

        return cmsSecKillAndSkuDtos;
    }

    @Override
    public List<CmsSecKillSkuDto> getSecKillSkus(Long secKillId,Long shopId) {
        List<CmsSecKillSkuDto> cmsSecKillSkuDtos = new ArrayList<>();

        Example exampleSku = new Example(CmsSecKillSku.class);
        exampleSku.setOrderByClause("sort");
        Example.Criteria criteriaSku = exampleSku.createCriteria();
        criteriaSku.andEqualTo("secKillId", secKillId);
        criteriaSku.andEqualTo("isEnable",true);

        List<CmsSecKillSku> cmsSecKillSkus = cmsSecKillSkuMapper.selectByExample(exampleSku);

        if (cmsSecKillSkus != null && cmsSecKillSkus.size() > 0) {
            // TODO 获取商品信息
            getSpecInfo(cmsSecKillSkus);

            for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
                CmsSecKillSkuDto cmsSecKillSkuDto = new CmsSecKillSkuDto();
                BeanUtils.copyProperties(cmsSecKillSku, cmsSecKillSkuDto);
                cmsSecKillSkuDtos.add(cmsSecKillSkuDto);
            }
            //2019.10.22 添加仓库城市名称、Id、CityId
            Long regionCityId = 0L;
            String regionCityName = "";
            Long wareHouseId = 0L;

            List<Long> shopInfoDtoList = new ArrayList<>();
            shopInfoDtoList.add(shopId);

            List<ShopInfoDto> shopRegion = smcFeignApi.getShopInfoByShopIds(shopInfoDtoList);


            if (shopRegion!=null && shopRegion.size()>0){
                regionCityId  = shopRegion.get(0).getRegionCityId();
                regionCityName = shopRegion.get(0).getRegionCityName();
            }

            Wrapper warehouseidWrapper= wmcFeignApi.getwarehouseidbyshopid(shopId);
            if (warehouseidWrapper!=null && warehouseidWrapper.getResult()!= null){
                wareHouseId = Long.valueOf(warehouseidWrapper.getResult().toString());
            }

            List<Long> specIds = cmsSecKillSkuDtos.stream().map(CmsSecKillSkuDto::getSpecId).collect(Collectors.toList());

            Map<String,Integer>  stockCounts = new HashMap<>();
            Wrapper  cityStockCountWrapper = wmcFeignApi.getWarehouseStockCount(wareHouseId,specIds);
            if (cityStockCountWrapper!=null && cityStockCountWrapper.getResult()!=null){
                 stockCounts = (Map)cityStockCountWrapper.getResult();
            }


            for(CmsSecKillSkuDto item :cmsSecKillSkuDtos){
                item.setWareCityId(regionCityId);
                item.setWareCityName(regionCityName);
                item.setWarehouseId(wareHouseId);

                Iterator iter = stockCounts.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    Object key = entry.getKey();
                    Object val = entry.getValue();
                    if (key.toString().equals(item.getSpecId().toString())){
                        item.setStockCount(Integer.valueOf(val.toString()));
                    }
                }

            }

        }
        return cmsSecKillSkuDtos;
    }

    private void getSpecInfo(List<CmsSecKillSku> cmsSecKillSkus) {
        // 获取商品信息
        String specIds = "";
        int i = 0;
        for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
            ++i;
            specIds += String.valueOf(cmsSecKillSku.getSpecId());
            if (i < cmsSecKillSkus.size()) {
                specIds += ",";
            }
        }
        List<ProdSpecInfoDto> specInfoDtosBySpecIds = pmcFeignApi.getSpecInfoDtosBySpecIds(specIds);

        if (specInfoDtosBySpecIds != null) {
            for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
                for (ProdSpecInfoDto prodSpecInfoDto : specInfoDtosBySpecIds) {
                    if (cmsSecKillSku.getSpecId().longValue() == prodSpecInfoDto.getSpecId().longValue()) {
                        cmsSecKillSku.setSpecName(prodSpecInfoDto.getSpecName());
                        cmsSecKillSku.setCategoryName(prodSpecInfoDto.getCategoryName());
                        cmsSecKillSku.setWeight(prodSpecInfoDto.getWeight());
                        cmsSecKillSku.setProductId(prodSpecInfoDto.getProductId());
                        //todo 20191029修改，将getSalePrice() 改为getGuidancePrice();
                        cmsSecKillSku.setMarketPrice(prodSpecInfoDto.getGuidancePrice());
                        //todo 20191029修改，将SecKillPrice 改为.getSalePrice();
                        if (cmsSecKillSku.getSecKillId().equals(3L)){
                            cmsSecKillSku.setSecKillPrice(prodSpecInfoDto.getStockPrice());
                        }else{
                            cmsSecKillSku.setSecKillPrice(prodSpecInfoDto.getSalePrice());
                        }

                        if (PublicUtil.isEmpty(cmsSecKillSku.getPicUrl())) {
                            cmsSecKillSku.setPicUrl(prodSpecInfoDto.getPicture());
                        }
                        break;
                    }
                }
            }
        }
    }
}
